from django.db import models
from django.utils import timezone


# Create your models here.
class FuturesHistory(models.Model):
    """期货商品历史行情模型"""
    id = models.AutoField(primary_key=True, verbose_name="主键")
    exchange = models.CharField(max_length=64, verbose_name="交易所")
    breed = models.CharField(max_length=64, verbose_name="品种")
    code = models.CharField(max_length=64, verbose_name="编码")
    code_complete = models.CharField(max_length=64, verbose_name="完整编码")
    ak_code = models.CharField(max_length=64, null=True, blank=True, verbose_name="ak_code")
    current = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True, verbose_name="当前")
    current_to_lowest = models.DecimalField(max_digits=8, decimal_places=3, null=True, blank=True,
                                            verbose_name="当前比最低")
    highest = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True, verbose_name="最高")
    minimum = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True, verbose_name="最低")
    min_date = models.IntegerField(null=True, blank=True, verbose_name="最小日期")
    lowest_to_highest = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True,
                                            verbose_name="最低/最高")
    current_to_highest = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True,
                                             verbose_name="当前/最高")
    trans_date = models.IntegerField(null=True, blank=True, verbose_name="交易日期")
    create_by = models.CharField(max_length=64, null=True, blank=True, verbose_name="创建人")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_by = models.CharField(max_length=64, null=True, blank=True, verbose_name="更新人")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    plan_buy_point = models.CharField(max_length=64, null=True, blank=True, verbose_name="计划买入点位")
    buy_point = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True, verbose_name="买入点位")
    buy_point_lowlest = models.DecimalField(max_digits=8, decimal_places=3, null=True, blank=True,
                                            verbose_name="买入比最低")
    remark = models.TextField(null=True, blank=True, verbose_name="备注")

    class Meta:
        managed = False  # 不允许Django管理表结构（表已存在）
        db_table = "futures_commodity_test"  # 关联到数据库中的表名
        verbose_name = "期货菜单"
        verbose_name_plural = "2.期货历史数据"


# Create your models here.
class FuturesCurrent(models.Model):
    """期货商品当天行情模型"""
    id = models.AutoField(primary_key=True, verbose_name="主键")
    exchange = models.CharField(max_length=64, verbose_name="交易所")
    breed = models.CharField(max_length=64, verbose_name="品种")
    code = models.CharField(max_length=64, verbose_name="编码")
    code_complete = models.CharField(max_length=64, verbose_name="完整编码")
    ak_code = models.CharField(max_length=64, null=True, blank=True, verbose_name="ak_code")
    current = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True, verbose_name="当前")
    current_to_lowest = models.DecimalField(max_digits=8, decimal_places=3, null=True, blank=True,
                                            verbose_name="当前比最低")
    highest = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True, verbose_name="最高")
    minimum = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True, verbose_name="最低")
    min_date = models.IntegerField(null=True, blank=True, verbose_name="最小日期")
    lowest_to_highest = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True,
                                            verbose_name="最低/最高")
    current_to_highest = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True,
                                             verbose_name="当前/最高")
    hold = models.IntegerField(null=True, blank=True, verbose_name="持仓量")
    launch_date = models.IntegerField(null=True, blank=True, verbose_name="上市日期")
    trans_date = models.IntegerField(null=True, blank=True, verbose_name="交易日期")
    create_by = models.CharField(max_length=64, null=True, blank=True, verbose_name="创建人")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_by = models.CharField(max_length=64, null=True, blank=True, verbose_name="更新人")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    plan_buy_point = models.CharField(max_length=64, null=True, blank=True, verbose_name="计划买入点位")
    buy_point = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True, verbose_name="买入点位")
    buy_point_lowlest = models.DecimalField(max_digits=8, decimal_places=3, null=True, blank=True,
                                            verbose_name="买入比最低")
    remark = models.TextField(null=True, blank=True, verbose_name="备注")

    class Meta:
        managed = False  # 不允许Django管理表结构（表已存在）
        db_table = "futures_current"  # 关联到数据库中的表名
        verbose_name = "期货菜单"
        verbose_name_plural = "1.期货当前数据"


class AkCodeConfig(models.Model):
    """AK代码配置表模型"""
    id = models.BigAutoField(primary_key=True, verbose_name="主键")
    ak_code = models.CharField(max_length=255, null=True, blank=True, verbose_name="akshare代码编号")
    exchange = models.CharField(max_length=255, null=True, blank=True, verbose_name="交易所")
    breed = models.CharField(max_length=255, null=True, blank=True, verbose_name="品种")
    code = models.CharField(max_length=255, null=True, blank=True, verbose_name="编码")
    code_complete = models.CharField(max_length=255, null=True, blank=True, verbose_name="完整编码")
    # is_pull 使用BooleanField映射bit(1)类型，默认值为True（对应b'1'）
    is_pull = models.BooleanField(default=True, verbose_name="是否拉取标记")
    create_by = models.CharField(max_length=255, null=True, blank=True, verbose_name="创建人")
    create_time = models.DateTimeField(null=True, blank=True, verbose_name="创建时间")
    update_by = models.CharField(max_length=255, null=True, blank=True, verbose_name="更新人")
    update_time = models.DateTimeField(null=True, blank=True, verbose_name="更新时间")

    class Meta:
        managed = False  # 不管理表结构（表已存在）
        db_table = "ak_code_config"  # 关联数据库表名
        verbose_name = "AK代码配置"
        verbose_name_plural = "3.AK代码配置管理"

    def save(self, *args, **kwargs):
        """重写保存方法，自动更新时间（如果需要）"""
        if not self.create_time:
            self.create_time = timezone.now()
        self.update_time = timezone.now()
        super().save(*args, **kwargs)

class ConfigDictionary(models.Model):
    """配置字典模型"""
    id = models.BigAutoField(primary_key=True, verbose_name="主键")
    dic_key = models.CharField(max_length=255, unique=True, verbose_name="字典键")
    dic_value = models.TextField(verbose_name="字典值")
    dic_remark = models.TextField(null=True, blank=True, verbose_name="字典备注")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        managed = False  # 不管理表结构（表已存在）
        db_table = "config_dic"  # 关联数据库表名
        verbose_name = "配置字典"
        verbose_name_plural = "4.配置字典管理"
